package com.hotel.action;


import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hotel.dto.Envelope;
import com.hotel.entity.AdminsEntity;
import com.hotel.entity.CheckInEntity;
import com.hotel.service.CheckInService;

@Controller
@RequestMapping("/checkIn")
public class CheckInAction {
	private CheckInService checkInService;
	public CheckInAction() {
		checkInService=new CheckInService();
	}
	@RequestMapping("/checkInList")
	public ModelAndView checkInList(HttpSession session,@RequestParam(value="page", required=false, defaultValue="1")int page){
		ModelAndView model= new ModelAndView("checkIn/checkInList");
		Envelope env=checkInService.getValidCheckInList(page);
		if(env.getCode()!=0){
			session.setAttribute("errorMessage",env.getMessage());
			return new ModelAndView("system/error");
		}
		model.addObject("checkIns", env.getObj());
		model.addObject("currentPage", page);
		return model;
	}
	@RequestMapping("/offLineCheckInMenu")
	public String offLineCheckInMenu(){
		return "checkIn/offLineCheck";
	}
	@RequestMapping("/onLineCheckInMenu")
	public String onLineCheckInMenu(){
		return "checkIn/onLineCheck";
	}
	@RequestMapping("/typeList")
	@ResponseBody
	public String getTypeList(){
		try{
			return checkInService.getTypeNameList().toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
	@RequestMapping("/freeRoomList")
	@ResponseBody
	public String getFreeRoomList(int id,String fromStr,String toStr){
		try{
			return checkInService.getFreeRoomByType(id,fromStr,toStr).toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
	@RequestMapping("/roomInfor")
	@ResponseBody
	public String getRoomInfor(int id){
		try{
			return checkInService.getRoomInfor(id).toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
	@RequestMapping(value="/lockRoom",method=RequestMethod.POST)
	@ResponseBody
	public String lockRoom(int id,int code){
		try{
			return checkInService.lockRoom(id, code).toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
	@RequestMapping(value="/offlineCheck",method=RequestMethod.POST)
	@ResponseBody
	public String offlineCheck(CheckInEntity entity,String dateStr1,String dateStr2,HttpSession session){
		AdminsEntity admin=(AdminsEntity) session.getAttribute("user");
		try{
			return checkInService.offLineCheckIn(entity, admin.getId(), dateStr1, dateStr2).toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
	@RequestMapping(value="/onlineCheck",method=RequestMethod.POST)
	@ResponseBody
	public String onlineCheck(HttpSession session,int orderId,String phone,String idCardNo,String zcomment){
		AdminsEntity admin=(AdminsEntity) session.getAttribute("user");
		try{
			return checkInService.onlineCheckIn(orderId, admin.getId(), phone, idCardNo, zcomment).toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
	@RequestMapping(value="/leaveRoomConfirm",method=RequestMethod.POST)
	@ResponseBody
	public String leaveRoomConfirm(int id){
		try{
			return checkInService.leaveRoomConfirm(id).toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
	@RequestMapping(value="/leaveRoom",method=RequestMethod.POST)
	@ResponseBody
	public String leaveRoom(int id){
		try{
			return checkInService.leaveRoom(id).toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
	@RequestMapping(value="/getReserve",method=RequestMethod.POST)
	@ResponseBody
	public String getReserve(int id,String password){
		try{
			return checkInService.getReserveInfor(id, password).toString();
		}catch (Exception e) {
			e.printStackTrace();
			return Envelope.systemError.toString();
		}
	}
}
